package com.boatAnchorAssistant.service.check;


import java.lang.reflect.Field;
import java.util.List;

import com.boatAnchorAssistant.pojo.check.Check;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import nirvana.core.domains.entity.DomainBaseEntity;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.IRepository;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * @author anni.zhu
 * @author anni.zhu@king-v.com
 * Created in 2024/11/13 11:22
 */
@Component
public class ClassUtil<T> {

    /**
     * 新增时排重
     * @param o
     * @param service
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public void addIsHave(T o, IRepository<T> service) throws IllegalArgumentException, IllegalAccessException {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();

        StringBuffer errorMas=new StringBuffer();

        for (Field field : o.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            String value = field.getType().toString();
            if (!field.getName().equals("serialVersionUID") && field.get(o) != null && !value.equals("boolean")) {
                boolean isCheck = field.isAnnotationPresent(Check.class);
                if (isCheck) {
                    //获取字段的英文名
                    String fieldName = field.getName();
                    //获取字段的中文名,用于错误提示
                    Column tableField = field.getAnnotation(Column.class);
                    String fieldValue = tableField.comment();

                    queryWrapper.eq("" + fieldName + "", field.get(o));
                    List<T> list = service.list(queryWrapper);
                    if (list.size() > 0) {
                        errorMas.append(fieldValue+"不能重复");
                    }
                }
            }
        }

        if (errorMas.length()>0){
            throw new BizException(errorMas.toString());
        }

    }

    /**
     * 更新时排重
     * @param o
     * @param service
     * @param id
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public void updateIsHave(T o, IRepository<T> service, Long id)
            throws IllegalArgumentException, IllegalAccessException {
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();

        StringBuffer errorMas=new StringBuffer();

        for (Field field : o.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            // 获取字段的值
            String value = field.getType().toString();
            if (!field.getName().equals("serialVersionUID") && field.get(o) != null && !value.equals("boolean")) {
                boolean isCheck = field.isAnnotationPresent(Check.class);
                if (isCheck) {
                    String fieldName = field.getName();

                    Column tableField = field.getAnnotation(Column.class);
                    String fieldValue = tableField.comment();

                    queryWrapper.eq("" + fieldName + "", field.get(o));
                    List<T> list = service.list(queryWrapper);
                    if (list.size() == 1) {
                        T g = list.get(0);
                        if (g != null && g instanceof DomainBaseEntity) {
                            DomainBaseEntity domainBaseEntity = (DomainBaseEntity) g;
                            if (!domainBaseEntity.getId().equals(id)) {
                                errorMas.append(fieldValue+"不能重复");
                            }
                        }
                    } else if (list.size() > 1) {
                        errorMas.append(fieldValue+"不能重复");
                    }
                }
            }
        }

        if (errorMas.length()>0){
            throw new BizException(errorMas.toString());
        }
    }
}
